/*
 * Copyright (c) 2011 by Konstantin Pavlov.
 */

package gwtlx.html5.client.workers;

import com.google.gwt.core.client.JavaScriptObject;

/**
 * Web Worker wrapper.
 *
 * @author Konstantin Pavlov
 * @version $LastChangedRevision: 39 $
 * @see <a href="http://dev.w3.org/html5/workers/">Web Workers</a>
 * @since 02.06.2010
 */
public class Worker extends JavaScriptObject {

    protected Worker() {
    }

    public static Worker as(JavaScriptObject javaScriptObject) {
        return (Worker) javaScriptObject;
    }

    public static native Worker create(String url) /*-{
        return @gwtlx.html5.client.workers.Worker::as(Lcom/google/gwt/core/client/JavaScriptObject;)(new Worker(url));
    }-*/;

    public final native void setEventListener(EventListener l)  /*-{
        this.onmessage = new function(event) {
            l.@gwtlx.html5.client.workers.Worker.EventListener::onMessage(Ljava/lang/Object;)(event);
        }
    }-*/;

    public final native void postMessage(String message) /*-{
        this.postMessage(message);
    }-*/;

    public interface EventListener {
        void onMessage(Object event);
    }
}
